/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.emergency.resources.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.emergency.resources.dto.ErEmergencyResourcesObjDTO;
import com.hopes.bizz.emergency.resources.entity.ErExpertEntity;
import com.hopes.bizz.emergency.resources.entity.ErMaterialCheckEntity;
import com.hopes.bizz.emergency.resources.entity.ErMaterialEntity;
import com.hopes.bizz.emergency.resources.mapper.ErMaterialMapper;
import com.hopes.bizz.emergency.resources.service.ErMaterialCheckService;
import com.hopes.bizz.emergency.resources.service.ErMaterialService;
import com.hopes.bizz.emergency.resources.vo.ErMaterialVO;
import com.hopes.boss.common.core.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * 应急物资
 *
 * @author cx
 * @date 2023-11-01 15:22:45
 */
@Service
public class ErMaterialServiceImpl extends ServiceImpl<ErMaterialMapper, ErMaterialEntity> implements ErMaterialService {

    @Autowired
    private  ErMaterialCheckService erMaterialCheckService;

    @Override
    public R erMaterialPage(Page page, ErMaterialEntity erMaterial) {
        LambdaQueryWrapper<ErMaterialEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(erMaterial.getMaterialEquipmentType()), ErMaterialEntity::getMaterialEquipmentType, erMaterial.getMaterialEquipmentType());
        wrapper.like(StrUtil.isNotBlank(erMaterial.getMaterialEquipmentName()), ErMaterialEntity::getMaterialEquipmentName, erMaterial.getMaterialEquipmentName());
        wrapper.like(StrUtil.isNotBlank(erMaterial.getUnitName()), ErMaterialEntity::getUnitName, erMaterial.getUnitName());
        Page pageInfo = this.page(page, wrapper);
        ArrayList<ErMaterialVO> voList = new ArrayList<>();
        for (Object record : pageInfo.getRecords()) {
            ErMaterialVO vo = new ErMaterialVO();
            BeanUtils.copyProperties(record, vo);
            List<ErMaterialCheckEntity> checkList = erMaterialCheckService.lambdaQuery()
                    .eq(ErMaterialCheckEntity::getMaterialId, vo.getId())
                    .orderByDesc(ErMaterialCheckEntity::getCheckDate)
                    .list();
            LocalDate latestCheckDate = checkList.stream().findFirst().map(ErMaterialCheckEntity::getCheckDate).orElse(null);
            if (latestCheckDate!=null){
                vo.setLatestCheckDate(latestCheckDate);
                Period interval = Period.between(LocalDate.now(), vo.getNextCheckDate());
                int days = interval.getDays();
                //间隔小于三天提示 三天内警告变橙色 未检查变红色 否则不变色
                if (days >= 0 && days <= 3) {
                    vo.setDateApproachingColour("orange");
                } else if (days < 0) {
                    vo.setDateApproachingColour("red");
                }
            }
            voList.add(vo);
        }
        pageInfo.setRecords(voList);
        return R.ok(pageInfo);
    }

    @Override
    public void erMaterialDataChange(ErMaterialCheckEntity erMaterialCheck) {
        LambdaUpdateWrapper<ErMaterialEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ErMaterialEntity::getId,erMaterialCheck.getMaterialId())
                .set(ErMaterialEntity::getIntactCondition,erMaterialCheck.getIntactCondition())
                .set(ErMaterialEntity::getNextCheckDate,erMaterialCheck.getNextCheckDate());
        this.update(updateWrapper);
    }

    @Override
    public R getListByUnitId(Page page, ErEmergencyResourcesObjDTO resourcesObjDTO) {
        LambdaQueryWrapper<ErMaterialEntity> queryWrapper = Wrappers.<ErMaterialEntity>lambdaQuery().eq(ObjUtil.isNotNull(resourcesObjDTO.getInitId()), ErMaterialEntity::getUnitId, resourcesObjDTO.getInitId());
        return R.ok(this.page(page,queryWrapper));
    }


}